package com.xinqi.modules.sys.menu.controller.admin;

import com.xinqi.common.base.constants.ServiceNameConstants;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.common.log.syslog.annotation.Log;
import com.xinqi.common.log.syslog.enums.BusinessType;
import com.xinqi.common.log.syslog.enums.OperatorType;
import com.xinqi.modules.sys.menu.dto.req.AddRoleMenuDTO;
import com.xinqi.modules.sys.menu.dto.req.MenuAllQueryDTO;
import com.xinqi.modules.sys.menu.dto.req.MenuCreateDTO;
import com.xinqi.modules.sys.menu.dto.req.MenuQueryDTO;
import com.xinqi.modules.sys.menu.dto.req.MenuTreeDTO;
import com.xinqi.modules.sys.menu.dto.req.MenuUpdateDTO;
import com.xinqi.modules.sys.menu.dto.rsp.MenuResultDTO;
import com.xinqi.modules.sys.menu.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统菜单WEB控制层
 *
 * @author: linkai
 * @date: 2023/04/01
 */
@Tag(name = "Admin-系统菜单", description = "系统菜单Admin接口")
@RestController
@RequestMapping("/admin/menu")
@RequiredArgsConstructor
public class MenuAdminController {
    private final MenuService menuService;

    /**
     * 系统菜单-分页
     */
    @Operation(summary = "系统菜单-分页")
    @GetMapping("/page")
    public R<PageInfo<MenuResultDTO>> page(@ParameterObject MenuQueryDTO dto) {
        PageInfo<MenuResultDTO> page = menuService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 系统菜单-详情
     */
    @Operation(summary = "系统菜单-详情")
    @GetMapping("/{id}")
    public R<MenuResultDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        MenuResultDTO dto = menuService.findById(id);
        return R.data(dto);
    }

    /**
     * 新增系统菜单
     */
    @Operation(summary = "新增系统菜单")
    @PostMapping
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增系统菜单", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Long> insert(@RequestBody MenuCreateDTO dto) {
        return R.data(menuService.insert(dto));
    }

    /**
     * 修改系统菜单
     */
    @Operation(summary = "修改系统菜单")
    @PutMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 修改系统菜单", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id, @RequestBody MenuUpdateDTO dto) {
        dto.setId(id);
        return R.data(menuService.updateById(dto));
    }

    /**
     * 删除系统菜单
     */
    @Operation(summary = "删除系统菜单")
    @DeleteMapping("/{id}")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 删除系统菜单", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(menuService.deleteById(id));
    }

    @Operation(summary = "根据菜单id获取下一级子菜单")
    @GetMapping("/{id}/child")
    public R<List<MenuResultDTO>> findChildByParentId(@Parameter(description = "id", example = "100001")
                                                      @PathVariable("id") Long parentId) {
        List<MenuResultDTO> resultList = menuService.findChildByParentId(parentId);
        return R.data(resultList);
    }

    @Operation(summary = "查询菜单树")
    @GetMapping("/tree")
    public R<List<MenuResultDTO>> findTree(@ParameterObject MenuTreeDTO req) {
        List<MenuResultDTO> resultList = menuService.findTree(req);
        return R.data(resultList);
    }

    @Operation(summary = "查询菜单ID")
    @GetMapping("/all/ids")
    public R<List<Long>> findAllIds(@ParameterObject MenuAllQueryDTO req) {
        return R.data(menuService.findAllIds(req));
    }

    /**
     * 查询全部菜单
     */
    @Operation(summary = "查询全部菜单")
    @GetMapping("/all")
    public R<List<MenuResultDTO>> findAll(@ParameterObject MenuAllQueryDTO req) {
        List<MenuResultDTO> list = menuService.findAll(req);
        return R.data(list);
    }

    /**
     * 查询用户菜单
     */
    @Operation(summary = "查询用户菜单")
    @GetMapping("/user-menu")
    public R<List<MenuResultDTO>> findUserMenu(@Parameter(description = "用户Id", example = "100001") Long userId) {
        List<MenuResultDTO> list = menuService.findUserMenu(userId);
        return R.data(list);
    }

    /**
     * 查询当前用户菜单
     */
    @Operation(summary = "查询当前用户菜单")
    @GetMapping("/current-user-menu")
    public R<List<MenuResultDTO>> findCurrentUserMenu() {
        return R.data(menuService.findCurrentUserMenu());
    }

    /**
     * 新增角色菜单
     *
     * @param dto 新增实体
     * @return 新增结果
     */
    @Operation(summary = "新增角色菜单")
    @PostMapping(value = "/add/role-menu")
    @Log(title = ServiceNameConstants.XQ_SYS + " -- 新增角色菜单", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    public R<Boolean> addRoleMenu(@Validated @RequestBody AddRoleMenuDTO dto) {
        return R.data(menuService.addRoleMenu(dto));
    }

    /**
     * 查询角色菜单
     *
     * @param roleId 角色ID
     * @return 返回菜单
     */
    @Operation(summary = "查询角色菜单")
    @GetMapping(value = "/role-menu/{role-id}")
    public R<List<MenuResultDTO>> getRoleMenu(@Parameter(description = "角色ID") @PathVariable("role-id") Long roleId) {
        return R.data(menuService.getRoleMenu(roleId));
    }

}
